package cn.lyw.seckillsession.controller.controller;

import cn.common.beancopy.util.BeanCopierUtil;
import cn.common.restresult.util.RestResult;
import cn.lyw.seckill.UserSeckill;
import cn.lyw.seckill.UserSeckillDTO;
import cn.lyw.seckillgoods.mapper.pojo.SmallMiSeckillGoods;
import cn.lyw.seckillsession.controller.dto.AddSessionDTO;
import cn.lyw.seckillsession.controller.dto.EditSessionDTO;
import cn.lyw.seckillsession.controller.vo.GetSessionAllVO;
import cn.lyw.seckillsession.controller.vo.GetSessionByIdVO;
import cn.lyw.seckillsession.controller.vo.GetSessionTodayVO;
import cn.lyw.seckillsession.service.bo.AddSessionBO;
import cn.lyw.seckillsession.service.bo.EditSessionBO;
import cn.lyw.seckillsession.service.domain.GetSessionAllDO;
import cn.lyw.seckillsession.service.domain.GetSessionByIdDO;
import cn.lyw.seckillsession.service.domain.GetSessionTodayDO;
import cn.lyw.seckillsession.service.service.SeckillSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author   lyw
 * @Description 秒杀场次controller
 * @Date 17:08 2021/7/23
 * @Param
 * @return
 */
@RestController
@RequestMapping("/seckillSession")
public class SeckillSessionController {


    @Autowired
    private SeckillSessionService seckillSessionService;

    @Autowired
    private UserSeckill userSeckill;

    /**
     * @Author   lyw
     * @Description 查询全部场次信息
     * @Date 17:10 2021/7/23
     * @Param
     * @return
     */
    @PostMapping("/getSessionAll")
    public RestResult<List<GetSessionAllVO>> getSessionAll(){

        //调用service查询全部方法
        List<GetSessionAllDO> sessionAll = seckillSessionService.getSessionAll();

        //转换参数
        List<GetSessionAllVO> getSessionAllVOS = BeanCopierUtil.copyList(sessionAll, GetSessionAllVO.class);

        return new RestResult<>(getSessionAllVOS);
    }



    /**
     * @Author   lyw
     * @Description 查询单个场次信息
     * @Date 17:34 2021/7/23
     * @Param
     * @return
     */
    @PostMapping("/getSessionById")
    public RestResult<GetSessionByIdVO> getSessionById(String sessionId){

        //调用service查询单个
        GetSessionByIdDO sessionById = seckillSessionService.getSessionById(sessionId);

        //转换参数
        GetSessionByIdVO getSessionByIdVO = BeanCopierUtil.copyObj(sessionById, GetSessionByIdVO.class);

        return new RestResult<>(getSessionByIdVO);
    }


    /**
     * @Author   lyw
     * @Description 根据id删除场次
     * @Date 17:41 2021/7/23
     * @Param
     * @return
     */
    @PostMapping("/delSessionById")
    public RestResult delSessionById(String sessionId){

        //调用service
        RestResult restResult = seckillSessionService.delSessionById(sessionId);

        return restResult;
    }


    /**
     * @Author   lyw
     * @Description  修改秒杀场次
     * @Date 17:48 2021/7/23
     * @Param
     * @return
     */
    @PostMapping("/editSession")
    public RestResult editSession(EditSessionDTO dto){

        //转换参数
        EditSessionBO editSessionBO = BeanCopierUtil.copyObj(dto, EditSessionBO.class);

        //调用service修改方法
        return seckillSessionService.editSession(editSessionBO);

    }


    /**
     * @Author   lyw
     * @Description  添加秒杀场次
     * @Date 17:48 2021/7/23
     * @Param
     * @return
     */
    @PostMapping("/addSession")
    public RestResult addSession(AddSessionDTO dto){

        //转换参数
        AddSessionBO addSessionBO = BeanCopierUtil.copyObj(dto, AddSessionBO.class);

        //调用service添加方法
        return seckillSessionService.addSession(addSessionBO);

    }


    /**
     * @Author   lyw
     * @Description 查询当天的秒杀场次
     * @Date 14:28 2021/7/24
     * @Param
     * @return
     */
    @PostMapping("/getSessionToday")
    public RestResult<List<GetSessionTodayVO>> getSessionToday(){

        //调用查询当天方法
        List<GetSessionTodayDO> sessionToday = seckillSessionService.getSessionToday();

        //转换返回值
        List<GetSessionTodayVO> getSessionTodayVOS = BeanCopierUtil.copyList(sessionToday, GetSessionTodayVO.class);

        return new RestResult<>(getSessionTodayVOS);
    }


    @PostMapping("/seckill")
    public RestResult<SmallMiSeckillGoods> seckill(@RequestBody UserSeckillDTO dto){

        return userSeckill.seckill(dto);

    }

}
